package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.addDish(dish);
        if(flavors!=null&& !flavors.isEmpty()){
            Long id = dish.getId();
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public PageResult getPage(DishPageQueryDTO dishPageQueryDTO) {
        //调用PageHelper
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //调用函数
        List<DishVO> list=dishMapper.getPage(dishPageQueryDTO);
        //封装解析
        Page<DishVO> p= (Page<DishVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
        //1.在售卖中的菜品无法删除
        for (Long id : ids) {
            DishVO dishVO = dishMapper.selectById(id);
            if(dishVO.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //如果该菜品包含在其他套餐中则不能删除
        List<Long> list=setmealMapper.getById(ids);
        if(!list.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除Dish表中的
         dishMapper.delete(ids);
        //删除dish_flavor表中的
        dishFlavorMapper.delet(ids);

    }

    @Override
    public void changeStatus(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    @Override
    public DishVO getById(Long id) {
        DishVO dishVO= dishMapper.selectById(id);
        List<DishFlavor> list=dishFlavorMapper.getById(id);
        dishVO.setFlavors(list);
        return dishVO;

    }

    @Transactional
    @Override
    public void update(DishVO dishVO) {
        //先更新dish表
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        dishMapper.update(dish);
        //在更新dish_flavor表
        List<DishFlavor> flavors = dishVO.getFlavors();

        //1.先删除
       dishFlavorMapper.deletByDishId(dishVO.getId());
        //然后在添加
        if(flavors!=null&& !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.addDishFlavor(flavors);
        }

    }

    @Override
    public List<Dish> getlist(Long categoryId) {
        Dish dish = Dish.builder().categoryId(categoryId).build();
        return dishMapper.getlist(dish);
    }

    @Override
    public List<DishVO> getlistall(Long categoryId) {
        //定义key的命名规则
        String key="dish_"+categoryId;
        //从缓存区查询菜
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        //判断是否为空
        if(list!=null&& !list.isEmpty()){
            return list;
        }
        //不为空则继续从数据库查
        Dish dish = Dish.builder().categoryId(categoryId).status(1).build();
        List<Dish> dishList = dishMapper.getlist(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        //将数据写入缓存区（redis）
        redisTemplate.opsForValue().set(key,dishVOList);
        return dishVOList;
    }
}
